//
//  DetailsNavBar.m
//  BestPurchase
//
//  Created by ZSMAC on 2018/10/12.
//  Copyright © 2018年 张帅. All rights reserved.
//

#import "DetailsNavBar.h"
@interface DetailsNavBar ()
@property(nonatomic,strong)MCustomButton *backBtn;
@property(nonatomic,strong)NSMutableArray * topBtns;
@property(nonatomic,retain)UIView * line;
@property(nonatomic,retain)UIView * selectLine;
@end
@implementation DetailsNavBar
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _topBtns=[[NSMutableArray alloc] init];
        [self uiSrt];
    }
    return self;
}
- (void)uiSrt {
    _backBtn = [MCustomButton buttonWithType:UIButtonTypeCustom];
    _backBtn.frame=CGRectMake(15, self.height-40, 35, 35);
    _backBtn.imageRect=CGRectMake(_backBtn.width/4,_backBtn.height/4, 15, 15);
    [_backBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
    _backBtn.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5];
    _backBtn.layer.cornerRadius=_backBtn.width/2;
    _backBtn.layer.masksToBounds=YES;
    _backBtn.titleLabel.font=Font(13);
    [_backBtn addTarget:self action:@selector(backBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_backBtn];
    NSArray * titles = @[@"宝贝",@"评价",@"详情",@"推荐",];
    CGFloat width = 40;
    for (int i=0; i<4; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        if (i==0) {
            [btn setTitleColor:[themeColor colorWithAlphaComponent:0] forState:UIControlStateNormal];
            btn.selected=YES;
        } else {
            UIColor *titleColor= [[UIColor blackColor] colorWithAlphaComponent:0];
            [btn setTitleColor:titleColor forState:UIControlStateNormal];
        }
        btn.frame=CGRectMake(i*width+_backBtn.width+(width*2), self.height-40, width, 35);
        NSString * title = titles[i];
        [btn setTitle:title forState:UIControlStateNormal];
      
        btn.backgroundColor=[UIColor clearColor];
        btn.titleLabel.font=Font(13);
        [btn addTarget:self action:@selector(topBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [_topBtns addObject:btn];
    }
    UIButton * btn = self.topBtns.firstObject;
    _selectLine = [[UIView alloc] initWithFrame:CGRectMake(btn.left, btn.bottom, 26, 1)];
    _selectLine.center=CGPointMake(btn.center.x, btn.bottom);
    [self addSubview:_selectLine];
    
    
    _line = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-0.5, self.width, 0.5)];
    _line.backgroundColor=[UIColor clearColor];
    [self addSubview:_line];
}
- (void)backBtnClicked:(UIButton *)btn {
    [[MPublicManager sharedPublicManager].currentControllr.navigationController popViewControllerAnimated:YES];
}
- (void)topBtnClicked:(UIButton *)btn {
    for (UIButton *topBtn in self.topBtns) {
        topBtn.selected=NO;
        [topBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    [btn setTitleColor:themeColor forState:UIControlStateNormal];
    btn.selected=YES;
    @WeakObj(self);
    [UIView animateWithDuration:0.5 animations:^{
        selfWeak.selectLine.center=CGPointMake(btn.center.x, btn.bottom);
    }];
}

- (void)setCurrentAlpha:(CGFloat)currentAlpha {
    _currentAlpha=currentAlpha;
    if (_currentAlpha<1) {
        self.backgroundColor =[MyRGB(247, 247, 247) colorWithAlphaComponent:_currentAlpha];
        
        UIColor *NormalColor = [[UIColor blackColor] colorWithAlphaComponent:_currentAlpha];
         UIColor *selectedColor = [themeColor colorWithAlphaComponent:_currentAlpha];//选中颜色
       
        for (UIButton *btn in self.topBtns) {
            if (btn.selected) {
                [btn setTitleColor:selectedColor forState:UIControlStateNormal];
            } else {
                [btn setTitleColor:NormalColor forState:UIControlStateNormal];
            }
        }
        _line.backgroundColor=[[UIColor groupTableViewBackgroundColor] colorWithAlphaComponent:_currentAlpha];
        _selectLine.backgroundColor=[themeColor colorWithAlphaComponent:_currentAlpha];
        
        _backBtn.backgroundColor=[_backBtn.backgroundColor colorWithAlphaComponent:(1-_currentAlpha)-0.5];
//        UIColor *textColor = [[UIColor whiteColor] colorWithAlphaComponent:1-_currentAlpha];
//        [_backBtn setTitleColor:textColor forState:UIControlStateNormal];
        [_backBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];

    }else {
        [_backBtn setImage:[UIImage imageNamed:@"back_black"] forState:UIControlStateNormal];
        self.backgroundColor =[MyRGB(247, 247, 247) colorWithAlphaComponent:_currentAlpha];
    }

}










@end
